package com.houtai.service.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.houtai.base.RetInfo;
import com.houtai.bean.TsJzAdmin;
import com.houtai.bean.TsProblem;
import com.houtai.bean.dto.problem.ProblemListDto;
import com.houtai.common.util.LogUtils;
import com.houtai.dao.TsJzAdminOperationDao;
import com.houtai.dao.TsProblemDao;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2017/7/28.
 */
@Service

public class ProblemService {
    private static Logger logger = Logger.getLogger(ProblemService.class);
    @Autowired
    private TsProblemDao tsProblemDao;


    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private TsJzAdminOperationDao tsJzAdminOperationDao;
    /**
     * @Purpose   查找常见问题
     * @param     page
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo findSchoolPage(ProblemListDto page, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":findSchoolPage:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            PageHelper.startPage(page.getCurrent_page(), page.getPage_size(), "arti_scort DESC, p_id desc");
            List<TsProblem> lists = tsProblemDao.findTsProblemList(page);
            PageInfo<TsProblem> list = new PageInfo<>(lists);
            Map<String, Object> map = new HashMap<>();
            map.put("list", list.getList());
            map.put("total_page", list.getPages());
            map.put("total_record", list.getTotal());
            map.put("current_page", list.getPageNum());
            map.put("page_size", list.getPageSize());
            map.put("arti_name", page.getArti_name());
            retInfo.setMark("0");
            retInfo.setTip("成功");
            retInfo.setObj(map);
            LogUtils.AdminOperation(redisTemplate ,tsJzAdminOperationDao,"查找常见问题" , request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");

        return retInfo;
    }
    /**
     * @Purpose   添加常见问题
     * @param     tsProblem
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo insertTsProblem(TsProblem tsProblem, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":insertTsProblem:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
            tsProblem.setModifier(tsJzAdmin.getUser_name());

            tsProblem.setCreater(tsJzAdmin.getUser_name());
            tsProblemDao.insertTsProblem(tsProblem);
            retInfo.setMark("0");
            retInfo.setTip("创建成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"添加常见问题,id:" + tsProblem.getP_id(),request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   修改常见问题
     * @param     tsProblem
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo updateTsProblem(TsProblem tsProblem, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateTsProblem:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
            tsProblem.setModifier(tsJzAdmin.getUser_name());
            tsProblemDao.updateTsProblem(tsProblem);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改常见问题,id:" + tsProblem.getP_id(),request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * @Purpose   删除常见问题
     * @param     p_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo deleteTsProblem(int p_id, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":deleteTsProblem:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tsProblemDao.updateTsProblemState(p_id);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"删除常见问题,id:" + p_id,request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    public TsProblem findTsProblemById(Integer p_id) {
        return tsProblemDao.findTsProblemDetail(p_id);
    }
    /**
     * @Purpose   修改联系我们
     * @param     arti_content
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    public RetInfo updateContant(String arti_content, HttpServletRequest request) {
        String logInfo = this.getClass().getName() + ":updateContant:";
        RetInfo retInfo = new RetInfo();
        logger.info("======" + logInfo + "begin======");
        try {
            tsProblemDao.updateTsProblemContent(arti_content);
            retInfo.setMark("0");
            retInfo.setTip("成功.");
            LogUtils.AdminOperation(redisTemplate,tsJzAdminOperationDao,"修改联系我们:" ,request);

        } catch (Exception e) {
            logger.error("======error:" + e.toString() + "======");
            retInfo.setMark("-1");
            retInfo.setTip("系统错误");
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
